package com.huli.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huli.domain.*;
import com.huli.dto.AdminDto;
import com.huli.dto.PagePropertyCompanyDto;
import com.huli.service.*;
import com.huli.utils.JwtUtils;
import com.huli.utils.Result;
import com.huli.vo.PagePropertyCompanyVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

@RestController
//@RequestMapping("/platform")
@Api(tags = "平台接口")
public class PlatformController {

    final IUserService userService;
    final ICarportService carportService;
    final ICarService carService;
    final IOwnerService ownerService;
    final ICommentService commentService;
    final IReplyService replyService;
    final IAppealService appealService;
    final IAdminService adminService;
    final IPropertyCompanyService propertyCompanyService;
    final IPropertyCompanyDeductService propertyCompanyDeductService;


    public PlatformController(IAdminService adminService, IUserService userService, ICarportService carportService, ICarService carService, IOwnerService ownerService, ICommentService commentService, IReplyService replyService, IAppealService appealService, IPropertyCompanyService propertyCompanyService, IPropertyCompanyDeductService propertyCompanyDeductService) {
        this.userService = userService;
        this.carportService = carportService;
        this.carService = carService;
        this.ownerService = ownerService;
        this.commentService = commentService;
        this.replyService = replyService;
        this.appealService = appealService;
        this.propertyCompanyService = propertyCompanyService;
        this.propertyCompanyDeductService = propertyCompanyDeductService;
        this.adminService = adminService;
    }

    @PostMapping("adminLogin")
    @ApiOperation("管理员登录")
    public Result adminLogin(@RequestBody AdminDto adminDto) {
        Admin admin = adminService.getOne(new QueryWrapper<Admin>()
                .eq("admin_account", adminDto.getAccount()));
        Assert.notNull(admin, "账号或密码不正确!");
        boolean b = admin.getAdminPassword().equals(adminDto.getPassword());
        Assert.isTrue(b, "账号或密码不正确");
        JwtUtils jwtUtils = new JwtUtils();

        return Result.successResult(jwtUtils.generateToken(admin.getAdminId()));
    }

    /**
     * @param userId 用户Id
     * @return
     */
    @PutMapping("passUser")
    @ApiOperation("审核用户")
    public Result passUser(@RequestParam("userId") Integer userId) {
        User user = new User();
        user.setUserId(userId);
        user.setUserCheck(2);
        boolean b = userService.updateById(user);
        Assert.isTrue(b, "审核用户通过失败!");
        return Result.successResult(null);
    }

    /**
     * @param ownerId 位主Id
     * @return
     */
    @PutMapping("passOwner")
    @ApiOperation("审核业主")
    public Result passOwner(@RequestParam("ownerId") Integer ownerId) {
        Owner owner = new Owner();
        owner.setOwnerId(ownerId);
        owner.setOwnerCheck(2);
        boolean b = ownerService.updateById(owner);
        Assert.isTrue(b, "审核位主通过失败!");
        return Result.successResult(null);
    }

    /**
     * @param carId 车辆ID
     * @return
     */
    @PutMapping("passCar")
    @ApiOperation("审核车辆")
    public Result passCar(@RequestParam("carId") Integer carId) {
        Car car = new Car();
        car.setCarId(carId);
        car.setCarCheck(2);
        boolean b = carService.updateById(car);
        Assert.isTrue(b, "审核车辆通过失败!");
        return Result.successResult(null);
    }

    /**
     * @param carportId 车位ID
     * @return
     */
    @PutMapping("passCarport")
    @ApiOperation("审核车位")
    public Result passCarport(@RequestParam("carportId") Integer carportId) {
        Carport carport = new Carport();
        carport.setCarportId(carportId);
        carport.setCarportCheck(2);
        boolean b = carportService.updateById(carport);
        Assert.isTrue(b, "审核车位通过失败!");
        return Result.successResult(null);
    }

    /**
     * @param commentId 评论表ID
     * @return
     */
    @DeleteMapping("delComment")
    @ApiOperation("删除评论")
    public Result delComment(@RequestParam("commentId") Integer commentId) {
        boolean b = commentService.removeById(commentId);
        Assert.isTrue(b, "删除评论失败");

        return Result.successResult(null);
    }

    /**
     * @param replyId 回复表ID
     * @return
     */
    @DeleteMapping("delReply")
    @ApiOperation("删除回复")
    public Result delReply(@RequestParam("replyId") Integer replyId) {
        boolean b = replyService.removeById(replyId);
        Assert.isTrue(b, "删除回复失败");

        return Result.successResult(null);
    }

    /**
     * @param appealId 申述表ID
     * @param status   1为同意,-1为不同意,0为默认
     * @return
     */
    @PutMapping("editAppeal")
    @ApiOperation("申诉表审核")
    public Result editAppeal(@RequestParam("appealId") Integer appealId, @RequestParam("status") Integer status) {
        Appeal appeal = new Appeal();
        appeal.setAppealId(appealId);
        appeal.setAppealStatus(status);
        boolean b = appealService.updateById(appeal);
        Assert.isTrue(b, "修改申述表状态失败!");

        return Result.successResult(null);
    }

    @PostMapping("selectPropertyCompany")
    @ApiOperation("查询物业公司")
    public Result selectPropertyCompany(@RequestBody PagePropertyCompanyDto companyDto) {
        IPage<PagePropertyCompanyVO> page = propertyCompanyService.selectPropertyCompany(
                new Page<>(companyDto.getPageIndex(), companyDto.getPageNum()),
                new QueryWrapper<PagePropertyCompanyVO>()
                        .like("property_company_name", companyDto.getPropertyCompanyName())
        );
        Assert.notNull(page, "查询物业失败!");
        return Result.successResult(page);
    }

    @PutMapping("editPropertyCompanyRate")
    @ApiOperation("修改物业抽成率")
    public Result editPropertyCompanyRate(@RequestParam("pcId") Integer pcId, @RequestParam("rate") Double rate) {
        boolean b = propertyCompanyDeductService.update(new UpdateWrapper<PropertyCompanyDeduct>()
                .eq("property_company_id", pcId).set("property_company_deduct_discount", rate));
        Assert.isTrue(b, "更新物业抽成率失败!");

        return Result.successResult(null);
    }


}
